package cn.xjfcloud.onedriveserver.util;

import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

public class UploadFileUtil {

	private static final ThreadPoolExecutor EXECUTOR = (ThreadPoolExecutor) Executors.newFixedThreadPool(10);
	private static ConcurrentHashMap<String, String> uploadingUploadIdMap = new ConcurrentHashMap<String, String>(10);

	public static ThreadPoolExecutor getUploadFileThreadPoolExecutor() {
		return EXECUTOR;
	}

	/**
	 * 
	 * @param uploadId
	 * @param runningThread
	 */
	public static void addRunningThread(String uploadId,String uuid) {
		uploadingUploadIdMap.put(uploadId, uuid);
	}

	public static void removeUUID(String uploadId) {
		uploadingUploadIdMap.remove(uploadId);
	}
	
	public static String getUUID(String uploadId) {
		return  uploadingUploadIdMap.get(uploadId);
	}
	/**
	 * 中断一个上传线程
	 * @param onedriveFileId
	 * @return
	 */
	public static boolean interruptRunningThread(String uploadId) {
		String uuid = uploadingUploadIdMap.get(uploadId);
		if(uuid == null) {
			return false;
		}
		//设置中断标记
		uploadingUploadIdMap.put(uploadId, UUID.randomUUID().toString());
		return true;
	}
}
